
import datetime

import numpy as np

def nor(avg, dev):
    random = round(np.random.normal(avg, dev), 2)
    if random > 0:
        return random


def getstatus(nowtime):
    # 判断当天是奇数天还是偶数天
    days = (nowtime - nowtime.replace(month=1, day=1, hour=0, minute=0, second=0, microsecond=0)).days + 1
    offset = 8 if days % 2 == 0 else 0
    hour_sep = [0, 3, 6, 9, 12, 15, 18, 21]
    ret = [0 for _ in range(18)]
    for i, hour in enumerate(hour_sep):
        if nowtime.replace(hour=hour, minute=0, second=0, microsecond=0) <= nowtime <= nowtime.replace(hour=hour,
                                                                                                       minute=17,
                                                                                                       second=0,
                                                                                                       microsecond=0):
            ret[i + offset] = 1
            if nowtime.replace(hour=hour, minute=0, second=0, microsecond=0) <= nowtime < nowtime.replace(hour=hour,
                                                                                                          minute=6,
                                                                                                          second=0,
                                                                                                          microsecond=0):
                ret[-2] = nor(7590, 157)
                ret[-1] = 0
            elif nowtime.replace(hour=hour, minute=6, second=0, microsecond=0) <= nowtime < nowtime.replace(hour=hour,
                                                                                                            minute=12,
                                                                                                            second=0,
                                                                                                            microsecond=0):
                ret[-2] = nor(7590, 157)
                ret[-1] = nor(1380, 55)
            else:
                ret[-2] = 0
                ret[-1] = nor(2350, 73)
            break
    return ret


ljll = 1000


def getBackFlushData(nowtime):
    # 格式化时间
    strtime = nowtime.strftime("%Y-%m-%d %H:%M:%S")
    strptime = nowtime.strptime(strtime, "%Y-%m-%d %H:%M:%S")

    devicelist = getstatus(strptime)
    print(devicelist)
    global ljll
    device01 = {
        "deviceId": "2022112901",
        "timestamp": strtime,
        "values": {
            "sc_zd8": nor(0.43, 0.15),
            "jsc_PH": nor(6.64, 0.25),
            "InstantaneousFlow": nor(3202, 700),
            "sc_znyl": nor(0.32, 0.2),
            "MF23": nor(10000, 2000),
            "GAS_LONG": 360,
            "PERIOD": 48,
            "MIX_LONG": 360,
            "MIX_GAS_FL": devicelist[16],  # 实际混冲气冲
            "WATER_LONG": 300,
            "GAS_FLOW": devicelist[16],  # 实际气冲流量
            "WATER_FLOW": devicelist[17],  # 实际水冲流量
            "MIX_WATER": devicelist[17],
            "CumulativeFlow": ljll + 3.57
        }
    }
    ljll = device01["values"]["CumulativeFlow"]
    device02 = {
        "deviceId": "2022112902",
        "timestamp": strtime,
        "values": {
            "FILTER_DIF": nor(21.81, 7.66),
            "sc_scyw1": nor(1.03, 0.11),
            "sc_csfkd": nor(45.75, 17),
            "Turbidity2": nor(0.035, 0.01),
            "filter_ty": devicelist[0]
        }
    }
    device03 = {
        "deviceId": "202211292",
        "timestamp": strtime,
        "values": {
            "FILTER_DIF": nor(21.81, 7.66),
            "sc_scyw1": nor(1.03, 0.11),
            "sc_csfkd": nor(45.75, 17),
            "Turbidity2": nor(0.035, 0.01),
            "filter_ty": devicelist[1]
        }
    }
    device04 = {
        "deviceId": "202211293",
        "timestamp": strtime,
        "values": {
            "FILTER_DIF": nor(21.81, 7.66),
            "sc_scyw1": nor(1.03, 0.11),
            "sc_csfkd": nor(45.75, 17),
            "Turbidity2": nor(0.035, 0.01),
            "filter_ty": devicelist[2]
        }
    }
    device05 = {
        "deviceId": "202211294",
        "timestamp": strtime,
        "values": {
            "FILTER_DIF": nor(21.81, 7.66),
            "sc_scyw1": nor(1.03, 0.11),
            "sc_csfkd": nor(45.75, 17),
            "Turbidity2": nor(0.035, 0.01),
            "filter_ty": devicelist[3]
        }
    }
    device06 = {
        "deviceId": "202211295",
        "timestamp": strtime,
        "values": {
            "FILTER_DIF": nor(21.81, 7.66),
            "sc_scyw1": nor(1.03, 0.11),
            "sc_csfkd": nor(45.75, 17),
            "filter_ty": devicelist[4]
        }
    }
    device07 = {
        "deviceId": "202211296",
        "timestamp": strtime,
        "values": {
            "FILTER_DIF": nor(21.81, 7.66),
            "sc_scyw1": nor(1.03, 0.11),
            "sc_csfkd": nor(45.75, 17),
            "filter_ty": devicelist[5]
        }
    }
    device08 = {
        "deviceId": "202211297",
        "timestamp": strtime,
        "values": {
            "FILTER_DIF": nor(21.81, 7.66),
            "sc_scyw1": nor(1.03, 0.11),
            "sc_csfkd": nor(45.75, 17),
            "filter_ty": devicelist[6]
        }
    }
    device09 = {
        "deviceId": "202211298",
        "timestamp": strtime,
        "values": {
            "FILTER_DIF": nor(21.81, 7.66),
            "sc_scyw1": nor(1.03, 0.11),
            "sc_csfkd": nor(45.75, 17),
            "filter_ty": devicelist[7]
        }
    }
    device10 = {
        "deviceId": "202211299",
        "timestamp": strtime,
        "values": {
            "FILTER_DIF": nor(21.81, 7.66),
            "sc_scyw1": nor(1.03, 0.11),
            "sc_csfkd": nor(45.75, 17),
            "filter_ty": devicelist[8]
        }
    }
    device11 = {
        "deviceId": "2022112910",
        "timestamp": strtime,
        "values": {
            "FILTER_DIF": nor(21.81, 7.66),
            "sc_scyw1": nor(1.03, 0.11),
            "sc_csfkd": nor(45.75, 17),
            "filter_ty": devicelist[9]
        }
    }
    device12 = {
        "deviceId": "2022112911",
        "timestamp": strtime,
        "values": {
            "FILTER_DIF": nor(21.81, 7.66),
            "sc_scyw1": nor(1.03, 0.11),
            "sc_csfkd": nor(45.75, 17),
            "filter_ty": devicelist[10]
        }
    }
    device13 = {
        "deviceId": "2022112912",
        "timestamp": strtime,
        "values": {
            "FILTER_DIF": nor(21.81, 7.66),
            "sc_scyw1": nor(1.03, 0.11),
            "sc_csfkd": nor(45.75, 17),
            "filter_ty": devicelist[11]
        }
    }
    device14 = {
        "deviceId": "2022112913",
        "timestamp": strtime,
        "values": {
            "FILTER_DIF": nor(21.81, 7.66),
            "sc_scyw1": nor(1.03, 0.11),
            "sc_csfkd": nor(45.75, 17),
            "filter_ty": devicelist[12]
        }
    }
    device15 = {
        "deviceId": "2022112914",
        "timestamp": strtime,
        "values": {
            "FILTER_DIF": nor(21.81, 7.66),
            "sc_scyw1": nor(1.03, 0.11),
            "sc_csfkd": nor(45.75, 17),
            "filter_ty": devicelist[13]
        }
    }
    device16 = {
        "deviceId": "2022112915",
        "timestamp": strtime,
        "values": {
            "FILTER_DIF": nor(21.81, 7.66),
            "sc_scyw1": nor(1.03, 0.11),
            "sc_csfkd": nor(45.75, 17),
            "filter_ty": devicelist[14]
        }
    }
    device17 = {
        "deviceId": "2022112916",
        "timestamp": strtime,
        "values": {
            "FILTER_DIF": nor(21.81, 7.66),
            "sc_scyw1": nor(1.03, 0.11),
            "sc_csfkd": nor(45.75, 17),
            "filter_ty": devicelist[15]
        }
    }
    device18 = {
        "deviceId": "2022112903",
        "timestamp": strtime,
        "values": {
            "running": 1,
            "stop": 0,
            "broke": 0
        }
    }
    device19 = {
        "deviceId": "2022112904",
        "timestamp": strtime,
        "values": {
            "running": 0,
            "stop": 1,
            "broke": 0
        }
    }
    deviceID = [device01, device02, device03, device04, device05, device06, device07, device08, device09, device10,
                device11,
                device12, device13, device14, device15, device16, device17, device18, device19]
    for i in range(len(deviceID)):
        deviceID[i]["ratioValues"] = deviceID[i]["values"]
    return deviceID
